import Vue from 'vue'
//1.引入 axios
import axios from 'axios'

const _self = new Vue();
//配置  http request 请求拦截器
axios.interceptors.request.use(
    request => {
        // console.log('axiosInstance request', request);
        return request
    },
    error => {
        return Promise.reject(error)
    });


//配置 http response 响应拦截器
axios.interceptors.response.use(
    response => {
        // console.log('axiosInstance response', response);
        return response;
    },
    error => {
        return Promise.reject(error);
    }
);
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
let config = {
    //配置超时
    timeout: 10000,
    responseType: "json",
    withCredentials: true, // 是否允许带cookie这些
    transformRequest: [function (data) {
        //转换请求方式，将请求的参数转换为 form-data
        let ret = '';
        for (let item in data) {
            ret += encodeURIComponent(item) + '=' + encodeURIComponent(data[item]) + '&'
        }
        return ret
    }],
    transformResponse: [function (data) {
        //转换data 不用每个请求都用 querystring.stringify
        // console.log('transformResponse', data);
        return data;
    }],
    headers: {
        "Content-Type": "application/x-www-form-urlencoded"
    }
};
export const IP = "localhost";
export const BASE_URL = "http://" + IP + ":9090/base-sys/";
export const IM_URL = "http://" + IP + ":9091/im-sys/";
export const COMMON_URL = "http://" + IP + ":9092/common-sys/";
export const FLOW_URL = "http://" + IP + ":9093/workflow-sys/";
//配置基础url
let axiosInstanceConfig = _.assign({}, config, {baseURL: BASE_URL});
let axiosInstanceJsonConfig = _.assign({}, config, {
    baseURL: BASE_URL,
    headers: {
        "Content-Type": "application/json;charset=UTF-8"
    },
    transformRequest: [
        function (data) {
            return JSON.stringify(data);
        }
    ],
});
let axiosIMConfig = _.assign({}, config, {baseURL: IM_URL});
let axiosCommonConfig = _.assign({}, config, {baseURL: COMMON_URL});
let axiosFlowConfig = _.assign({}, config, {baseURL: FLOW_URL});
//创建 axios实例
export const axiosBaseInstance = axios.create(axiosInstanceConfig);
export const axiosBaseInstanceJson = axios.create(axiosInstanceJsonConfig);
export const axiosIMInstance = axios.create(axiosIMConfig);
export const axiosCommonInstance = axios.create(axiosCommonConfig);
export const axiosFlowInstance = axios.create(axiosFlowConfig);


/**
 * 封装get方法
 * @param axiosInstance
 * @param url
 * @param params
 * @returns {Promise}
 */
export function $httpGet(axiosInstance, url, params = {}) {
    return new Promise((resolve, reject) => {
        axiosInstance.get(url, {
            params: params
        }).then(response => {
            resolve(response.data);
        }).catch(error => {
            _self.$errorMsg("网络请求错误");
            reject(error)
        })
    })
}

/**
 * 封装post请求
 * @param axiosInstance
 * @param url
 * @param data
 * @returns {Promise}
 */

export function $httpPost(axiosInstance, url, data = {}) {
    return new Promise((resolve, reject) => {
        axiosInstance.post(url, data)
            .then(response => {
                resolve(response.data);
            }).catch((error) => {
            _self.$errorMsg("网络请求错误");
            reject(error);
        });
    })
}


/**
 * 封装put请求
 * @param axiosInstance
 * @param url
 * @param data
 * @returns {Promise}
 */
export function $httpPut(axiosInstance, url, data = {}) {
    return new Promise((resolve, reject) => {
        axiosInstance.put(url, data)
            .then(response => {
                resolve(response.data);
            }).catch((error) => {
            _self.$errorMsg("网络请求错误");
            reject(error);
        });
    })
}

/**
 * 封装 delete 请求
 * @param axiosInstance
 * @param url
 * @param params
 * @returns {Promise}
 */
export function $httpDelete(axiosInstance, url, params = {}) {
    return new Promise((resolve, reject) => {
        axiosInstance.delete(url, {
            params: params
        }).then(response => {
            resolve(response.data);
        }).catch((error) => {
            _self.$errorMsg("网络请求错误");
            reject(error);
        })
    })
}

